{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "204bcb05-a556-4d97-890c-ad0a28892102",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['Requirement already satisfied: mcap in ./venv/lib/python3.10/site-packages (1.1.1)',\n",
       " 'Requirement already satisfied: zstandard in ./venv/lib/python3.10/site-packages (from mcap) (0.22.0)',\n",
       " 'Requirement already satisfied: lz4 in ./venv/lib/python3.10/site-packages (from mcap) (4.3.3)']"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "!!pip install mcap"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "1e62c9d4-a045-436c-96a0-33c92f6ee737",
   "metadata": {},
   "outputs": [],
   "source": [
    "import json\n",
    "import sys\n",
    "from time import time_ns,sleep\n",
    "\n",
    "from mcap.writer import Writer,CompressionType\n",
    "\n",
    "ONE_HOUR_IN_NANOSECONDS = 3600000000000\n",
    "\n",
    "compression_types = {\n",
    "    \"LZ4\": CompressionType.LZ4,\n",
    "    \"NONE\": CompressionType.NONE,\n",
    "    \"ZSTD\": CompressionType.ZSTD,\n",
    "\n",
    "}\n",
    "\n",
    "for compression_name, _compression_type in compression_types.items():\n",
    "\n",
    "    with open(f\"basic_{compression_name}.mcap\", \"wb\") as stream:\n",
    "        writer = Writer(stream,compression=_compression_type)\n",
    "    \n",
    "        writer.start()\n",
    "    \n",
    "        schema_id = writer.register_schema(\n",
    "            name=\"sample\",\n",
    "            encoding=\"jsonschema\",\n",
    "            data=json.dumps(\n",
    "                {\n",
    "                    \"type\": \"object\",\n",
    "                    \"properties\": {\n",
    "                        \"value\": {\n",
    "                            \"type\": \"integer\",\n",
    "                        }\n",
    "                    },\n",
    "                }\n",
    "            ).encode('utf-8'),\n",
    "        )\n",
    "    \n",
    "        channel_id = writer.register_channel(\n",
    "            schema_id=schema_id,\n",
    "            topic=\"integer_topic\",\n",
    "            message_encoding=\"json\",\n",
    "        )\n",
    "    \n",
    "    \n",
    "        for i in range(10):\n",
    "            sleep(1)\n",
    "            writer.add_message(\n",
    "                channel_id=channel_id,\n",
    "                log_time=ONE_HOUR_IN_NANOSECONDS * i,\n",
    "                data=json.dumps({\"value\": i},ensure_ascii=False).encode('utf-8'),\n",
    "                publish_time=ONE_HOUR_IN_NANOSECONDS * i,\n",
    "                sequence=i\n",
    "            )\n",
    "    \n",
    "        writer.finish()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "69f16eca-193d-403b-a54a-ebe3cd935887",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "db6e6ed4-d2d8-4f96-b0ff-2e35372aa95d",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
